#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Text;
using System.Collections.Generic;
using SimpleJson;
using System;
using Excel;
using System.Data;

[System.Reflection.Obfuscation(Exclude = true)]
public class ILRuntimeDllToBytes
{

    [MenuItem("操作/转换dll为bytes并删除")]
    static void DllToBytes()
    {
        string path0 = Application.dataPath + "/StreamingAssets/HotFix_Project.dll";

        string path2 = Application.dataPath + "/StreamingAssets/HotFix_Project.pdb";

        string path1 = Application.dataPath + "/Project/scriptdll/HotScriptdata.bytes";

        string path3 = Application.dataPath + "/Project/scriptdll/HotScriptdata_pdb.bytes";

        if (File.Exists(path0))
        {
            byte[] arr = File.ReadAllBytes(path0);
            File.WriteAllBytes(path1, arr);
            File.Delete(path0);
            Debug.Log("～～～～～begin～～～～～");
            Debug.Log("热更新代码 dllToBytes 转换完毕！");
            Debug.Log("Light tower still here！");
            Debug.Log("～～～～～end～～～～～");
        }
        else
        {
            Debug.Log("～～～～～begin～～～～～");
            Debug.Log(path0);
            Debug.Log("这个dll文件不存在啊！需要首先生成一个热更的dll并存放到此文件夹下。");
            Debug.Log("～～～～～end～～～～～");
        }

        if (File.Exists(path2))
        {
            byte[] arr = File.ReadAllBytes(path2);
            File.WriteAllBytes(path3, arr);
            File.Delete(path2);
            Debug.Log("～～～～～pdb 转换完毕～～～～～");
        }
        else
        {
            Debug.Log("～～～～～pdb 不存在～～～～～");
        }

        AssetDatabase.Refresh();
    }

    [MenuItem("操作/打包前清除streaming文件夹中dll和pdb")]
    static void ClearDll()
    {
        string path0 = Application.dataPath + "/StreamingAssets/HotFix_Project.dll";
        if (File.Exists(path0))
            File.Delete(path0);

        string path1 = Application.dataPath + "/StreamingAssets/HotFix_Project.pdb";
        if (File.Exists(path1))
            File.Delete(path1);

        //string path2 = Application.dataPath + "/StreamingAssets/UnityEngine.UI.dll";
        //if (File.Exists(path2))
        //    File.Delete(path2);

        // string path3 = Application.dataPath + "/StreamingAssets/UnityEngine.UI.pdb";
        //if (File.Exists(path3))
        //    File.Delete(path3);

    }


    [MenuItem("操作/从Excel导出json数据")]
    static void ExcelToJsonData()
    {
        //资源列表
        ExportBuffFile();
        ExportBulletFile();
        //ExportNaKaFile();
        AutoExportFild("NaKa","NaKa");
        ExportWarDollsFile();
        AutoExportFild("ADHistory", "ADHistory");
        Debug.Log("从Excel导出资源数据 已经完成");
    }


    static void AutoExportFild(string tableName, string outName)
    {
        string file = Application.dataPath + "/Project/res/data.xlsx";
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        // 表
        DataTable table = result.Tables[tableName];
        // 列
        int columnsS1 = table.Columns.Count;
        // 行
        int rowsS1 = table.Rows.Count;

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();

        List<string> valueName = new List<string>();
        for (int j = 0; j< columnsS1; j++)
        {
            string str = table.Rows[1][j].ToString();
            if (string.IsNullOrEmpty(str))
                break;
            else
                valueName.Add(str);
        }

        List<string> valueType = new List<string>();
        for (int i = 0; i < valueName.Count; i++)
        {
            string str = table.Rows[2][i].ToString();
            valueType.Add(str);
        }


        for (int i = 3; i < rowsS1; i++)
        {
            string teststr = table.Rows[i][0].ToString();
            if (string.IsNullOrEmpty(teststr))
                break;

            if (!long.TryParse(teststr, out long index))
                break;

            JsonObject obj = new JsonObject();

            for ( int j=0; j<valueName.Count; j++)
            {
                if(valueType[j] == "int")
                    obj.Add(valueName[j], int.Parse(table.Rows[i][j].ToString()));
                else if(valueType[j] == "long")
                    obj.Add(valueName[j], long.Parse(table.Rows[i][j].ToString()));
                else if (valueType[j] == "float")
                    obj.Add(valueName[j], float.Parse(table.Rows[i][j].ToString()));
                else if (valueType[j] == "double")
                    obj.Add(valueName[j], double.Parse(table.Rows[i][j].ToString()));
                else if (valueType[j] == "string")
                    obj.Add(valueName[j], table.Rows[i][j].ToString());
                else
                    obj.Add(valueName[j], table.Rows[i][j].ToString());
            }

            jsonArray.Add(obj);
        }

        jsonObject.Add("its", jsonArray);

        byte[] resbytes = Encoding.UTF8.GetBytes(jsonObject.ToString());

        string path = Application.dataPath + "/Project/res/"+ outName+ ".json";
        File.WriteAllBytes(path, resbytes);

        excelReader.Close();
        stream.Close();


    }


    static void ExportBuffFile()
    {
        string file = Application.dataPath + "/Project/res/data.xlsx";
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        // 表
        DataTable table = result.Tables["Buff"];
        // 列
        int columnsS1 = table.Columns.Count;
        // 行
        int rowsS1 = table.Rows.Count;

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();

        for (int i = 1; i < rowsS1; i++)
        {
            string teststr = table.Rows[i][0].ToString();
            if (string.IsNullOrEmpty(teststr))
                break;
            long index = 0;
            if (!long.TryParse(teststr, out index))
                break;

            JsonObject obj = new JsonObject();

            obj.Add("index", index);
            obj.Add("name", table.Rows[i][1].ToString());
            obj.Add("nameE", table.Rows[i][2].ToString());
            obj.Add("des", table.Rows[i][3].ToString());
            obj.Add("desE", table.Rows[i][4].ToString());
            obj.Add("type", int.Parse(table.Rows[i][5].ToString()));
            obj.Add("energyAtk", float.Parse(table.Rows[i][6].ToString()));
            obj.Add("energyDef", float.Parse(table.Rows[i][7].ToString()));
            obj.Add("lastT", float.Parse(table.Rows[i][8].ToString()));
            obj.Add("modleAssetIndex", table.Rows[i][9].ToString());
           
            jsonArray.Add(obj);
        }

        jsonObject.Add("its", jsonArray);

        byte[] resbytes = Encoding.UTF8.GetBytes(jsonObject.ToString());

        string path = Application.dataPath + "/Project/res/buff.json";
        File.WriteAllBytes(path, resbytes);

        excelReader.Close();
        stream.Close();
    }

    static void ExportBulletFile()
    {
        string file = Application.dataPath + "/Project/res/data.xlsx";
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        // 表
        DataTable table = result.Tables["Bullet"];
        // 列
        int columnsS1 = table.Columns.Count;
        // 行
        int rowsS1 = table.Rows.Count;

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();

        for (int i = 1; i < rowsS1; i++)
        {
            string teststr = table.Rows[i][0].ToString();
            if (string.IsNullOrEmpty(teststr))
                break;
            long index = 0;
            if (!long.TryParse(teststr, out index))
                break;

            JsonObject obj = new JsonObject();

            obj.Add("index", index);
            obj.Add("name", table.Rows[i][1].ToString());
            obj.Add("nameE", table.Rows[i][2].ToString());
            obj.Add("des", table.Rows[i][3].ToString());
            obj.Add("desE", table.Rows[i][4].ToString());
            obj.Add("type", int.Parse(table.Rows[i][5].ToString()));
            obj.Add("icon", table.Rows[i][6].ToString());
            obj.Add("modleAssetIndex", table.Rows[i][7].ToString());

            jsonArray.Add(obj);
        }

        jsonObject.Add("its", jsonArray);

        byte[] resbytes = Encoding.UTF8.GetBytes(jsonObject.ToString());

        string path = Application.dataPath + "/Project/res/bullet.json";
        File.WriteAllBytes(path, resbytes);

        excelReader.Close();
        stream.Close();
    }

    static void ExportNaKaFile()
    {
        string file = Application.dataPath + "/Project/res/data.xlsx";
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        // 表
        DataTable table = result.Tables["NaKa"];
        // 列
        int columnsS1 = table.Columns.Count;
        // 行
        int rowsS1 = table.Rows.Count;

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();

        for (int i = 1; i < rowsS1; i++)
        {
            string teststr = table.Rows[i][0].ToString();
            if (string.IsNullOrEmpty(teststr))
                break;
            long index = 0;
            if (!long.TryParse(teststr, out index))
                break;

            JsonObject obj = new JsonObject();

            obj.Add("index", index);
            obj.Add("name", table.Rows[i][1].ToString());
            obj.Add("nameE", table.Rows[i][2].ToString());
            obj.Add("des", table.Rows[i][3].ToString());
            obj.Add("desE", table.Rows[i][4].ToString());
            obj.Add("type", int.Parse(table.Rows[i][5].ToString()));
            obj.Add("fightType", int.Parse(table.Rows[i][6].ToString()));
            obj.Add("assistType", int.Parse(table.Rows[i][7].ToString()));
            obj.Add("icon", table.Rows[i][8].ToString());
            obj.Add("bigicon", table.Rows[i][9].ToString());

            jsonArray.Add(obj);
        }

        jsonObject.Add("its", jsonArray);

        byte[] resbytes = Encoding.UTF8.GetBytes(jsonObject.ToString());

        string path = Application.dataPath + "/Project/res/naka.json";
        File.WriteAllBytes(path, resbytes);

        excelReader.Close();
        stream.Close();
    }

    static void ExportWarDollsFile()
    {
        string file = Application.dataPath + "/Project/res/data.xlsx";
        FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        // 表
        DataTable table = result.Tables["WarDolls"];
        // 列
        int columnsS1 = table.Columns.Count;
        // 行
        int rowsS1 = table.Rows.Count;

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();

        for (int i = 1; i < rowsS1; i++)
        {
            string teststr = table.Rows[i][0].ToString();
            if (string.IsNullOrEmpty(teststr))
                break;
            long index = 0;
            if (!long.TryParse(teststr, out index))
                break;

            JsonObject obj = new JsonObject();

            obj.Add("index", index);
            obj.Add("name", table.Rows[i][1].ToString());
            obj.Add("nameE", table.Rows[i][2].ToString());
            obj.Add("des", table.Rows[i][3].ToString());
            obj.Add("desE", table.Rows[i][4].ToString());
            obj.Add("type", int.Parse(table.Rows[i][5].ToString()));
            obj.Add("Atk", float.Parse(table.Rows[i][6].ToString()));
            obj.Add("Def", float.Parse(table.Rows[i][7].ToString()));
            obj.Add("EDef", float.Parse(table.Rows[i][8].ToString()));
            obj.Add("Dodge", float.Parse(table.Rows[i][9].ToString()));
            obj.Add("bullet", int.Parse(table.Rows[i][10].ToString()));
            obj.Add("icon", table.Rows[i][11].ToString());
            obj.Add("modleAssetIndex", table.Rows[i][12].ToString());

            jsonArray.Add(obj);
        }

        jsonObject.Add("its", jsonArray);

        byte[] resbytes = Encoding.UTF8.GetBytes(jsonObject.ToString());

        string path = Application.dataPath + "/Project/res/warDolls.json";
        File.WriteAllBytes(path, resbytes);

        excelReader.Close();
        stream.Close();
    }


    //[MenuItem("实用操作/拷贝pod文件到ios目录下")]
    //static void CopyPodToIOSFold()
    //{
    //    string path0 = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + "podfile";
    //    Debug.Log("path0 " + path0);

    //    //string path1 = Application.dataPath.Substring(0, Application.dataPath.Length - 6) + "/ios/podfile";
    //    Debug.Log("path1 " + path1);

    //    File.Copy(path0, path1, true);
    //}
}
#endif